﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using VitCode.Web.UI.WebControls;

namespace AMS.Web.BackStageManagement
{
    public partial class UserInsert : Page
    {
        BaseDataManager m_Help = new BaseDataManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            if (!IsPostBack)
            {
                if (Request.Cookies["Code"] == null)
                {
                    Response.Redirect("Defalut.aspx");
                }
               
             

                string sql = "select Id,Name from Frame_UserGroup";
                DataTable dt = m_Help.GetDataTableBySql(sql);

                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        DropDownList_UserGroup.DataSource = dt;
                        DropDownList_UserGroup.DataTextField = "Name";
                        DropDownList_UserGroup.DataValueField = "Id";
                         DropDownList_UserGroup.DataBind();
                        DropDownList_UserGroup.Items.Add(new ListItem("", ""));
                     
                        dt.Dispose();
                    }
                }

                sql = "select Id,Name from Frame_Role";
                DataTable dt2 = m_Help.GetDataTableBySql(sql);
                if (dt2 != null)
                {
                    if (dt2.Rows.Count > 0)
                    {
                        DropDownList_Role.DataSource = dt2;
                        DropDownList_Role.DataTextField = "Name";
                        DropDownList_Role.DataValueField = "Id";
                        DropDownList_Role.DataBind();
                         DropDownList_Role.Items.Add(new ListItem("", ""));
                         
                        dt2.Dispose();
                    }
                }
                if (Request.QueryString["Id"] != null)
                {
                    labelId.Text = Request.QueryString["Id"].Trim();

                    User currentUser;

                    IList objlist = m_Help.GetObjectList(new User().GetType().Name, "Id", labelId.Text.Trim());
                    if (objlist != null && objlist.Count > 0)
                    {
                        currentUser = (User) objlist[0];

                        ZLTextBox_Code.Text = currentUser.Code;
                        ZLTextBox_Number.Text = currentUser.Number;
                        ZLTextBox_Mobile.Text = currentUser.Mobile;
                        ZLTextBox_Email.Text = currentUser.Email;
                        ZLTextBox_AdminRealName.Text = currentUser.Name;
                        if (currentUser.UserGroup != null)
                        {
                            string id = currentUser.UserGroup.Id.ToString().Trim();
                            if (DropDownList_UserGroup.Items.Count > 0 &&
                                DropDownList_UserGroup.Items.FindByValue(id) != null)
                            {
                                DropDownList_UserGroup.Items.FindByValue(id).Selected = true;
                            }
                            else
                            {
                                DropDownList_UserGroup.Items.FindByValue("").Selected = true;
                            }

                        }


                        IList objList2 = m_Help.GetObjectList(new UserRoleRelation().GetType().Name, "UserId",
                                                              labelId.Text);
                        if (objList2 != null && objList2.Count > 0)
                        {
                            var currentRoleRelation = (UserRoleRelation) objList2[0];
                            if (currentRoleRelation.RoleId.ToString().Length == 36)
                            {
                                Guid guidRoleId = currentRoleRelation.RoleId;
                                if (DropDownList_Role.Items.Count > 0 &&
                                    DropDownList_Role.Items.FindByValue(guidRoleId.ToString()) != null)
                                {
                                    DropDownList_Role.Items.FindByValue(guidRoleId.ToString()).Selected = true;
                                }
                                else
                                {
                                    DropDownList_Role.Items.FindByValue("").Selected = true;
                                }

                            }
                        }



                    }
                }
                else
                {
                    lblAddUserTip.Text = "*新增用户的默认密码是:123456";
                }
            }
        }



        protected void btnCancel_Click(object sender, EventArgs e)
        {

            Session["RoleUserWinClose"] = "Closed";
            Window.Close(new[] { "Closed" });
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strCode = ZLTextBox_Code.Text;
            string strNumber = ZLTextBox_Number.Text;
            string strMobile = ZLTextBox_Mobile.Text;
            string strEmail = ZLTextBox_Email.Text;
            string strName = ZLTextBox_AdminRealName.Text;
            var objUser = new User();
            if (labelId.Text != "" && labelId.Text.Length == 36)
            {
                string hql = " from User where Code ='" + ZLTextBox_Code.Text.Trim() + "' and Id !='" +
                       labelId.Text.Trim() + "'";
                IList userList = m_Help.Session.CreateQuery(hql).List();
                if (userList != null && userList.Count > 0)
                {
                    Alert.MessageBox("用户编号已存在！");
                    return;
                }

                IList objlist = m_Help.GetObjectList(new User().GetType().Name, "Id", labelId.Text.Trim());
                if (objlist != null && objlist.Count > 0)
                {
                    objUser = (User)objlist[0];
                    objUser.Name = strName;
                    objUser.Mobile = strMobile;
                    objUser.Email = strEmail;
                    objUser.Number = strNumber;
                    objUser.Code = strCode;
                }
                IList objList = m_Help.GetObjectList(new UserRoleRelation().GetType().Name, "UserId", labelId.Text);
                if (objList != null && objList.Count > 0)
                {
                    var currentRoleRelation = (UserRoleRelation)objList[0];
                    m_Help.Delete(currentRoleRelation);
                }

            }
            else
            {
                //控制用户数量， 不是Demo版则注释此代码˴���
                var scObjErrMsg = new StringCollection();
                if (DemoVersionManager.CheckIsDemoVersion("User", scObjErrMsg))
                {
                    if (scObjErrMsg.Count > 0)
                    {
                        var sb = new StringBuilder();
                        for (int i = 0; i < scObjErrMsg.Count; i++)
                            sb.Append(scObjErrMsg[i] + "\r\n");
                        Alert.MessageBox(sb.ToString());
                    }
                    return;
                }

                IList userList = m_Help.GetObjectList(new User().GetType().Name, "Code", ZLTextBox_Code.Text.Trim());
                if (userList != null && userList.Count > 0)
                {
                    Alert.MessageBox("用户编号已存在！");
                    return;
                }
                objUser.Code = strCode;
                objUser.Name = strName;
                objUser.Mobile = strMobile;
                objUser.Email = strEmail;
                objUser.Number = strNumber;
                objUser.Password = publ.VitEnctyptString("123456");
            }
 
            
           
            if (DropDownList_UserGroup.Items.Count > 0)
            {
                if (DropDownList_UserGroup.SelectedItem.Text.Trim().Length > 0)
                {
                    string strUserGroup = DropDownList_UserGroup.SelectedItem.Value.Trim();

                    IList objList = m_Help.GetObjectList(new VitCode.Castle.Entities.UserGroup().GetType().Name, "Id", strUserGroup);
                    if (objList != null && objList.Count > 0)
                    {
                        var currentUserGroup = (VitCode.Castle.Entities.UserGroup)objList[0];
                        objUser.UserGroup = currentUserGroup;

                    }
                }
                else
                {
                    objUser.UserGroup = null;

                }

            }
            bool access = m_Help.SaveOrUpdate(objUser);
            if (DropDownList_Role.Items.Count > 0)
            {
                if (DropDownList_Role.SelectedItem.Text.Trim().Length > 0)
                {
                    Guid guidRoleId = new Guid(DropDownList_Role.SelectedItem.Value.Trim());
                    IList objList = m_Help.GetObjectList(new User().GetType().Name, "Code", ZLTextBox_Code.Text);
                    if (objList != null && objList.Count > 0)
                    {
                        var currentUser = (User)objList[0];
                        Guid guidUserId = currentUser.Id;
                        var currentUserRoleRelation = new UserRoleRelation();
                        currentUserRoleRelation.RoleId = guidRoleId;
                        currentUserRoleRelation.UserId = guidUserId;
                        access = m_Help.SaveOrUpdate(currentUserRoleRelation);
                    }

                }
              
                  
                
            }
           

            if (access)
                Page.RegisterClientScriptBlock("信息提示", "<script language='javascript'>alert('保存成功！')</script>");

            Session["RoleUserWinClose"] = "Closed";
            Window.Close(new[] { "Closed" });
        }


    }
}
